/** 获取购物车数据 */
export function fetchCartGroupData(id) {
  return new Promise((resolve) => {
    resolve(wx.cloud.database().collection('book-cart').where({
      userid: id
    }).get())
  });
}

/** 加入购物车 */
export function addToCart(userid, book) {
  wx.cloud.database().collection('book-cart').where({userid, bookid: book._id}).get().then(res=>{
    if(res.data.length > 0){
      wx.showToast({
        title: '已经在购物车了',
        duration: 500
      })
    }else{
      wx.cloud.database().collection('book-cart').add({
        data:{
          title: book.title,
          bookid: book._id,
          price: book.price,
          primaryImage: book.primaryImage,
          userid,
          isSelected: 0,
          author: book.author
        },
        success:res=>{
          wx.showToast({
            title: '添加成功',
            duration: 500
          })
        }
      })
    }
  })
}
